package cn.sciento.farm.devicemanager.common.util.qiniu;

import cn.sciento.farm.devicemanager.common.util.PropertiesFileUtil;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.model.FetchRet;
import com.qiniu.util.Auth;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author wumu
 * @version V1.0
 * @date 5/11/18
 * @time 11:06 AM
 */
public class QiniuBucketUtil {
    private static Logger logger = LoggerFactory.getLogger(QiniuBucketUtil.class);


    //设置好账号的ACCESS_KEY和SECRET_KEY
    private static String accessKey = PropertiesFileUtil.getInstance(QiniuConstant.CONFIG_PATH).get(QiniuConstant.SERVER_ACCESS);
    private static String secretKey = PropertiesFileUtil.getInstance(QiniuConstant.CONFIG_PATH).get(QiniuConstant.SERVER_SECRET);
    private static String bucketName = PropertiesFileUtil.getInstance(QiniuConstant.CONFIG_PATH).get(QiniuConstant.SERVER_BUCKET);
    private static String serverUrl = PropertiesFileUtil.getInstance(QiniuConstant.CONFIG_PATH).get(QiniuConstant.SERVER_URL);


    private static Zone z = Zone.autoZone();
    private static Configuration c = new Configuration(z);

    private static Auth auth = Auth.create(accessKey, secretKey);
    private static BucketManager bucketManager = new BucketManager(auth, c);

    public static FetchRet fetch(String remoteSrcUrl, String bucket, String key) {
        //抓取网络资源到空间
        try {
            FetchRet fetchRet = bucketManager.fetch(remoteSrcUrl, bucket, key);
            return fetchRet;
        } catch (QiniuException ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static FetchRet fetch(String remoteSrcUrl, String key) {
        return fetch(remoteSrcUrl, bucketName, key);
    }

    //删除文件
    public static Response delete(String bucket, String key) {
        try {
            Response response = bucketManager.delete(bucket, key);
            return response;
        } catch (QiniuException ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static Response delete(String key) {
        return delete(bucketName, key);
    }

    //设置过期时间
    public static Response setExpiredTime(String bucket, String key, int days) {
        try {
            Response response = bucketManager.deleteAfterDays(bucket, key, days);
            return response;
        } catch (QiniuException ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static Response setExpiredTime(String key, int days) {
        return setExpiredTime(bucketName, key, days);
    }

    //获取文件列表
    public static BucketManager.FileListIterator getFileList(String bucket, String prefix, int limit, String delimiter) {
        if (prefix == null) {
            prefix = "";
        }
        if (limit == 0) {
            limit = 1000;
        }
        if (delimiter == null) {
            delimiter = "";
        }
        BucketManager.FileListIterator fileListIterator = bucketManager.createFileListIterator(bucket, prefix, limit, delimiter);
        return fileListIterator;
    }

    public static BucketManager.FileListIterator getFileList(String prefix) {
        return getFileList(bucketName, prefix, 1000, "");
    }

    /**
     * 单次批量请求的文件数量不得超过1000
     *
     * @param bucket
     * @param keyList
     * @return
     */
    public static Response batchDelete(String bucket, String[] keyList) {
        try {

            BucketManager.BatchOperations batchOperations = new BucketManager.BatchOperations();
            batchOperations.addDeleteOp(bucket, keyList);
            Response response = bucketManager.batch(batchOperations);
            return response;
        } catch (QiniuException ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static Response batchDelete(String[] keyList) {
        return batchDelete(bucketName, keyList);
    }


    public static Response move(String fromBucket, String fromKey, String toBucket, String toKey) {
        try {
            bucketManager.copy(fromBucket, fromKey, toBucket, toKey);
        } catch (QiniuException ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static Response move(String fromKey, String toKey) {
        return move(bucketName, fromKey, bucketName, toKey);
    }


}
